﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web.UI;

namespace ControlExplorer.samples.CellMerging
{
  public partial class Overview : SpreadDemoPage
  {

    protected void Page_Load(object sender, System.EventArgs e)
    {
      if (!Page.IsPostBack)
      {
         this.FpSpread1.UseClipboard = false;
        //Set the database connection
        FpSpread1.DataSource = GetOrderDetails();
        Page.DataBind();

        GetSchedule();

        //Set the widths
        FpSpread1.ActiveSheetView.Columns[0].Width = 60;
        FpSpread1.ActiveSheetView.Columns[1].Width = 350;

        FpSpread1.ActiveSheetView.PageSize = 20;

        //Init with merged cells
        MergeCells(true);
        MergeSchedule(true);
      }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {

    }
    #endregion

    protected void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
    {
      MergeCells(CheckBox1.Checked);
    }

    private void MergeCells(bool fpMerge)
    {
      if (fpMerge)
        //Merge
        FpSpread1.Sheets[0].SetColumnMerge(0, FarPoint.Web.Spread.Model.MergePolicy.Always);

      else
        //Un-Merge
        FpSpread1.Sheets[0].SetColumnMerge(0, FarPoint.Web.Spread.Model.MergePolicy.None);
    }

    private void GetSchedule()
    {
      int row = 0;

      FpSpread2.Sheets[0].Rows.Count = 4;
      FpSpread2.Sheets[0].Columns.Count = 6;
      FpSpread2.Sheets[0].CellSpacing = 1;

      FpSpread2.Sheets[0].DefaultRowHeight = 40;

      FpSpread2.Sheets[0].RowHeader.Cells[0, 0].Text = "ABC";
      FpSpread2.Sheets[0].RowHeader.Cells[1, 0].Text = "CBS";
      FpSpread2.Sheets[0].RowHeader.Cells[2, 0].Text = "FOX";
      FpSpread2.Sheets[0].RowHeader.Cells[3, 0].Text = "NBC";

      FpSpread2.Sheets[0].RowHeader.Width = 50;

      FpSpread2.Sheets[0].ColumnHeader.Cells[0, 0].Text = "时间 8:00";
      FpSpread2.Sheets[0].ColumnHeader.Cells[0, 1].Text = "时间 8:30";
      FpSpread2.Sheets[0].ColumnHeader.Cells[0, 2].Text = "时间 9:00";
      FpSpread2.Sheets[0].ColumnHeader.Cells[0, 3].Text = "时间 9:30";
      FpSpread2.Sheets[0].ColumnHeader.Cells[0, 4].Text = "时间 10:00";
      FpSpread2.Sheets[0].ColumnHeader.Cells[0, 5].Text = "时间 10:30";

      row = 0;
      //ABC
      //8:00
      FpSpread2.Sheets[0].Cells[row, 0].Text = "Extreme Makeover";
      //8:30
      FpSpread2.Sheets[0].Cells[row, 1].Text = "Extreme Makeover";
      //9:00
      FpSpread2.Sheets[0].Cells[row, 2].Text = "life as we know it";
      //9:30
      FpSpread2.Sheets[0].Cells[row, 3].Text = "life as we know it";
      //10:00
      FpSpread2.Sheets[0].Cells[row, 4].Text = "PrimeTime";
      //10:30
      FpSpread2.Sheets[0].Cells[row, 5].Text = "PrimeTime";

      row = 1;
      //CBS
      //8:00
      FpSpread2.Sheets[0].Cells[row, 0].Text = "Survivor";
      //8:30
      FpSpread2.Sheets[0].Cells[row, 1].Text = "Survivor";
      //9:00
      FpSpread2.Sheets[0].Cells[row, 2].Text = "CSI:";
      //9:30
      FpSpread2.Sheets[0].Cells[row, 3].Text = "CSI:";
      //10:00
      FpSpread2.Sheets[0].Cells[row, 4].Text = "Without a Trace";
      //10:30
      FpSpread2.Sheets[0].Cells[row, 5].Text = "Without a Trace";

      row = 2;
      //FOX
      //8:00
      FpSpread2.Sheets[0].Cells[row, 0].Text = "The O.C";
      //8:30
      FpSpread2.Sheets[0].Cells[row, 1].Text = "The O.C";
      //9:00
      FpSpread2.Sheets[0].Cells[row, 2].Text = "North Shore";
      //9:30
      FpSpread2.Sheets[0].Cells[row, 3].Text = "North Shore";
      //10:00
      FpSpread2.Sheets[0].Cells[row, 4].Text = "10PM News";
      //10:30
      FpSpread2.Sheets[0].Cells[row, 5].Text = "Extra";

      row = 3;
      //NBC
      //8:00
      FpSpread2.Sheets[0].Cells[row, 0].Text = "Joey";
      //8:30
      FpSpread2.Sheets[0].Cells[row, 1].Text = "Will & Grace";
      //9:00
      FpSpread2.Sheets[0].Cells[row, 2].Text = "The Apprentice";
      //9:30
      FpSpread2.Sheets[0].Cells[row, 3].Text = "The Apprentice";
      //10:00
      FpSpread2.Sheets[0].Cells[row, 4].Text = "ER";
      //10:30
      FpSpread2.Sheets[0].Cells[row, 5].Text = "ER";
    }

    protected void CheckBox2_CheckedChanged(object sender, System.EventArgs e)
    {
      MergeSchedule(CheckBox2.Checked);
    }

    private void MergeSchedule(bool fpMerge)
    {
      if (fpMerge)
        //Merge
        FpSpread2.Sheets[0].SetRowMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.Always);

      else
        //Un-Merge
        FpSpread2.Sheets[0].SetRowMerge(-1, FarPoint.Web.Spread.Model.MergePolicy.None);
    }

    const string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb;Persist Security Info=True";
    DataSet GetOrderDetails()
    {
      string sqlstr =
@"SELECT TOP 20 
        od.OrderID as 订单号, 
        p.ProductName as 产品名, 
        od.Quantity as 数量, 
        od.UnitPrice as 单价
  FROM [Order Details] as OD 
  INNER JOIN PRODUCTS as P ON od.ProductID = P.ProductID 
  ORDER BY OrderID ASC;";

      DataSet oDetails = new DataSet();
      using (OleDbConnection myconn = new OleDbConnection(constr))
      {
        OleDbDataAdapter sqlAdapter1 = new OleDbDataAdapter(sqlstr, myconn);
        sqlAdapter1.Fill(oDetails, "Order Details");
      }

      return oDetails;
    }
  }
}